package com.xuzimian.globaldemo.mq.rabbitmq.producer;

import com.xuzimian.globaldemo.mq.rabbitmq.constants.Config;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.junit.jupiter.api.Test;

/***
 * @Description rabbitmq jms客户端，mqtt协议的发布订阅(广播模式)的生产者
 * @author xzm
 */
public class JMSTopicProducer {

    @Test
    public void startProducer() {
        MqttClient mqttClient = null;
        try {
            mqttClient = new MqttClient("tcp://" + Config.MQServerConect.HOST + ":" + Config.MQServerConect.PORT_MQTT, "xzm");
            MqttConnectOptions options = new MqttConnectOptions();
            options.setCleanSession(true);
            options.setConnectionTimeout(20);
            options.setKeepAliveInterval(30);
            options.setUserName("/auagtddev_dy:" + Config.MQServerConect.USERNAME);
            options.setPassword(Config.MQServerConect.PASSWORD.toCharArray());
            mqttClient.connect(options);

            String topic1 = "test";
            mqttClient.publish(topic1, new MqttMessage("hello world".getBytes()));
        } catch (MqttException e) {
        } finally {
            //关闭资源
            if (null != mqttClient) {
                try {
                    mqttClient.close();
                } catch (MqttException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
